###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Remove.pm.pl -> Remove module                                               #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Remove;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Mail.pm.pl";
require "StaticPages.pm.pl";

use strict;

require "Output/Remove.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::Remove->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  unless ($SL::QUERY{'RID'}) {
    $self->show(DB => $in{'DB'}, ERROR => "MISSING-RID");
    return 1;
  }

  my $Resource = $in{'DB'}->BinarySelect(
    TABLE   => "Resources",
    KEY     => $SL::QUERY{'RID'}
  );
  unless ($Resource) {
    $self->show(DB => $in{'DB'}, ERROR => "INVALID-RID");
    return 1;
  }

  if ($SL::QUERY{'PASSWORD'} ne $Resource->{'PASSWORD'}) {
    $self->show(DB => $in{'DB'}, ERROR => "INVALID-PASSWORD");
    return 1;
  }

  my $Category = $in{'DB'}->BinarySelect(
    TABLE   => "Categories",
    KEY     => $Resource->{'CATEGORY'}
  );

  my $Flags = &Standard::ArrayToHash([ split(/,/, $Resource->{'FLAGS'}) ]);

  if ($Flags->{'DELETED'}) {
    $self->show(DB => $in{'DB'}, ERROR => "ALREADY-DELETED");
    return 1;
  }

  #----------------------------------------------------------------------#
  # Updating data...                                                     #

  if ($GENERAL->{'VALIDATE_REM_RESOURCES'}) {
    (
      $in{'DB'}->Update(
        TABLE   => "Resources",
        VALUES  => {
          STATUS  => "0",
          FLAGS   => ($Resource->{'FLAGS'} ? $Resource->{'FLAGS'}."," : "").",DELETED"
        },
        KEY     => $SL::QUERY{'RID'}
      )
    ) || &Error::Error("SL", MESSAGE => "Error updating record. $in{'DB'}->{'ERROR'}");
  } else {
    (
      $in{'DB'}->Delete(
        TABLE   => "Resources",
        KEYS    => [$SL::QUERY{'RID'}]
      )
    ) || &Error::Error("SL", MESSAGE => "Error deleting records. $in{'DB'}->{'ERROR'}");
    
    if ($Resource->{'UPLOAD_FILENAME'} && -e "$SYSTEM->{'PUBLIC_PATH'}/Upload/$Resource->{'UPLOAD_FILENAME'}") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Upload/$Resource->{'UPLOAD_FILENAME'}")
        || &Error::CGIError("Error deleting image upload file. $!", "$SYSTEM->{'PUBLIC_PATH'}/Upload/$Resource->{'UPLOAD_FILENAME'}");
    }
    
    my ($Rates, $RatesIndex) = $in{'DB'}->Query(
      TABLE   => "Rates",
      WHERE   => {
        RID => [$SL::QUERY{'RID'}]
      },
      MATCH   => "ANY"
    );
    my @RatID;
    foreach my $rate (@{ $Rates }) {
      push(@RatID, $rate->{'ID'});
    }

    if (scalar(@RatID) >= 1) {
      (
        $in{'DB'}->Delete(
          TABLE   => "Rates",
          KEYS    => \@RatID
        )
      ) || &Error::Error("CP", MESSAGE => "Error deleting records. $in{'DB'}->{'ERROR'}");
    }

    my ($Reviews, $ReviewsIndex) = $in{'DB'}->Query(
      TABLE   => "Reviews",
      WHERE   => {
        RID => [$SL::QUERY{'RID'}]
      },
      MATCH   => "ANY"
    );
    my @RevID;
    foreach my $review (@{ $Reviews }) {
      push(@RevID, $review->{'ID'});
    }

    if (scalar(@RevID) >= 1) {  
      (
        $in{'DB'}->Delete(
          TABLE   => "Reviews",
          KEYS    => \@RevID
        )
      ) || &Error::Error("CP", MESSAGE => "Error deleting records. $in{'DB'}->{'ERROR'}");
    }    
  }
  
  if ($Resource->{'STATUS'}) {
    (
      $in{'DB'}->Update(
        TABLE   => "Categories",
        VALUES  => {
          RESOURCES => "--\${RESOURCES}"
        },
        KEY     => $Resource->{'CATEGORY'}
      )
    ) || &Error::Error("SL", MESSAGE => "Error updating record. $in{'DB'}->{'ERROR'}");
  }
  
  my $StaticPages = StaticPages->new(DB => $in{'DB'});
  
  $StaticPages->Update(TYPE => "Detail", INPUT => $Resource)
    || &Error::Error("SL", MESSAGE => "Error updating static pages. $StaticPages->{'ERROR'}");
  
  $StaticPages->Remove(TYPE => "Detail", KEYS => [$SL::QUERY{'RID'}])
    || &Error::Error("SL", MESSAGE => "Error updating static pages. $StaticPages->{'ERROR'}");

  my %INPUT;
  
  $INPUT{'RESOURCE'} = $Resource;
  $INPUT{'CATEGORY'} = $Category;

  my $Skin = Skin::Remove->new();

  if ($GENERAL->{'NOTIFY_ADMIN_OF_REMOVE'}) {
    my %Message = $Skin->email(type => "admin", input => \%INPUT);
    my $Mail = Mail->new();
       $Mail->Send(%Message) || &Error::Error("SL", MESSAGE => "Error sending email to admin. $Mail->{'ERROR'}");
  }

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);

  return 1;
}

1;